<template>
  <div :class="['chat-message', `chat-message-${message.type}`]">
    <!-- 用户消息 -->
    <div v-if="message.type === 'user'" class="message-user">
      <div class="message-content">
        <div class="message-text">{{ message.content }}</div>
        <div class="message-time">{{ formatTime(message.timestamp) }}</div>
      </div>
      <div class="message-avatar">
        <el-avatar :size="36">
          <el-icon><User /></el-icon>
        </el-avatar>
      </div>
    </div>

    <!-- AI助手消息 -->
    <div v-else-if="message.type === 'assistant'" class="message-assistant">
      <div class="message-avatar">
        <el-avatar :size="36" :style="{ backgroundColor: message.error ? '#f56c6c' : '#409eff' }">
          <el-icon v-if="message.loading"><Loading /></el-icon>
          <el-icon v-else-if="message.error"><Warning /></el-icon>
          <el-icon v-else><Avatar /></el-icon>
        </el-avatar>
      </div>
      <div class="message-content">
        <div v-if="message.loading" class="message-loading">
          <el-text>AI正在思考中...</el-text>
          <div class="loading-dots">
            <span></span>
            <span></span>
            <span></span>
          </div>
        </div>
        <div v-else class="message-text" v-html="renderMarkdown(message.content)"></div>
        
        <!-- 数据库结果展示 -->
        <div v-if="message.data?.databaseResult && !message.loading" class="database-result">
          <DatabaseResult :result="message.data.databaseResult" />
        </div>
        
        <!-- 意图解析结果（详细模式） -->
        <div v-if="message.data?.intentResult && !message.loading" class="intent-result">
          <IntentResult :result="message.data.intentResult" />
        </div>
        
        <!-- 执行时间 -->
        <div v-if="message.data?.executionTime && !message.loading" class="execution-time">
          <el-text size="small" type="info">
            执行时间: {{ message.data.executionTime }}ms
          </el-text>
        </div>
        
        <div v-if="!message.loading" class="message-time">{{ formatTime(message.timestamp) }}</div>
      </div>
    </div>

    <!-- 系统消息 -->
    <div v-else-if="message.type === 'system'" class="message-system">
      <div class="system-content">
        <el-icon><InfoFilled /></el-icon>
        <div class="system-text" v-html="renderMarkdown(message.content)"></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { marked } from 'marked'
import {
  User,
  Avatar,
  Loading,
  Warning,
  InfoFilled
} from '@element-plus/icons-vue'
import type { MessageItem } from '@/types'
import DatabaseResult from '@/components/DatabaseResult.vue'
import IntentResult from '@/components/IntentResult.vue'
import dayjs from 'dayjs'

interface Props {
  message: MessageItem
}

const props = defineProps<Props>()

// 格式化时间
function formatTime(timestamp: number): string {
  return dayjs(timestamp).format('HH:mm:ss')
}

// 渲染Markdown
function renderMarkdown(content: string): string {
  try {
    return marked(content, {
      breaks: true,
      gfm: true
    })
  } catch (error) {
    console.error('Markdown渲染失败:', error)
    return content.replace(/\n/g, '<br>')
  }
}
</script>

<style scoped>
.chat-message {
  width: 100%;
  margin-bottom: 16px;
}

.message-user,
.message-assistant {
  display: flex;
  gap: 12px;
  max-width: 100%;
}

.message-user {
  justify-content: flex-end;
}

.message-user .message-content {
  background: #409eff;
  color: white;
  border-radius: 18px 18px 6px 18px;
  padding: 12px 16px;
  max-width: 70%;
  word-wrap: break-word;
}

.message-assistant .message-content {
  background: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 18px 18px 18px 6px;
  padding: 12px 16px;
  max-width: 80%;
  word-wrap: break-word;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.message-text {
  line-height: 1.6;
  margin-bottom: 8px;
}

.message-text:last-child {
  margin-bottom: 0;
}

.message-time {
  font-size: 12px;
  opacity: 0.7;
  text-align: right;
  margin-top: 4px;
}

.message-loading {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #409eff;
}

.loading-dots {
  display: flex;
  gap: 4px;
}

.loading-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #409eff;
  animation: loading-bounce 1.4s infinite ease-in-out both;
}

.loading-dots span:nth-child(1) { animation-delay: -0.32s; }
.loading-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes loading-bounce {
  0%, 80%, 100% {
    transform: scale(0);
  }
  40% {
    transform: scale(1);
  }
}

.message-system {
  display: flex;
  justify-content: center;
  margin: 16px 0;
}

.system-content {
  background: #f0f9ff;
  border: 1px solid #b3d8ff;
  border-radius: 8px;
  padding: 12px 16px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  max-width: 80%;
  color: #1d4ed8;
}

.system-text {
  line-height: 1.6;
}

.database-result,
.intent-result {
  margin: 12px 0;
  border-top: 1px solid #e4e7ed;
  padding-top: 12px;
}

.execution-time {
  margin-top: 8px;
  text-align: right;
}

/* Markdown样式 */
.message-content :deep(h1),
.message-content :deep(h2),
.message-content :deep(h3) {
  margin: 12px 0 8px 0;
  font-weight: 600;
}

.message-content :deep(p) {
  margin: 8px 0;
  line-height: 1.6;
}

.message-content :deep(ul),
.message-content :deep(ol) {
  margin: 8px 0;
  padding-left: 20px;
}

.message-content :deep(li) {
  margin: 4px 0;
}

.message-content :deep(code) {
  background: #f1f2f6;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Monaco', 'Consolas', monospace;
  font-size: 0.9em;
}

.message-content :deep(pre) {
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin: 12px 0;
}

.message-content :deep(pre code) {
  background: none;
  padding: 0;
  border-radius: 0;
}

.message-content :deep(blockquote) {
  border-left: 4px solid #409eff;
  padding-left: 12px;
  margin: 12px 0;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .message-user .message-content,
  .message-assistant .message-content {
    max-width: 85%;
  }
  
  .system-content {
    max-width: 90%;
  }
}
</style>